<?php
class categoryAuthModel extends hopeCmsModel
{

	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
    {
        return '{category_auth}';
    }

    public function getAuth($cat_id, $role_id, $action)
    {
        $auth = $this->findByAttribute($action, array('AND', 'cat_id='.$cat_id, 'role_id='.$role_id));
        return isset($auth[$action]) ? $auth[$action] : false;
    }

    public function saveCategoryRoleAuth($cat_id)
    {
        $auth_name = array(
            'look', 'add', 'edit', 'delete', 'sort', 'move'
        );
        $rids  = roleModel::model()->findsByAttribute('rid');
        foreach ($rids as $rid) {
            $this->cat_id  = $cat_id;
            $this->role_id = $rid['rid'];
            $name = 'role_'.$rid['rid'];
            if ($rid['rid'] == 1) {
                $this->look   = 1;
                $this->add    = 1;
                $this->edit   = 1;
                $this->delete = 1;
                $this->sort   = 1;
                $this->move   = 1;
                unset($_POST[$name]);
            } else {
                foreach ($_POST[$name] as $key => $role_auth) {
                    $this->{$auth_name[$key]} = $role_auth;
                }
                unset($_POST[$name]);
            }
            $this->insert();
            $this->setIsNew(true);
        }
    }

    public function updateCategoryRoleAuth($cat_id)
    {
        $auth_name = array(
            'look', 'add', 'edit', 'delete', 'sort', 'move'
        );
        $rids  = roleModel::model()->findsByAttribute('rid');
        foreach ($rids as $rid) {
            $this->cat_id  = $cat_id;
            $this->role_id = $rid['rid'];
            $name = 'role_'.$rid['rid'];
            if ($rid['rid'] == 1) {
                $this->look   = 1;
                $this->add    = 1;
                $this->edit   = 1;
                $this->delete = 1;
                $this->sort   = 1;
                $this->move   = 1;
                unset($_POST[$name]);
            } else {
                foreach ($_POST[$name] as $key => $role_auth) {
                    $this->{$auth_name[$key]} = $role_auth;
                }
                unset($_POST[$name]);
            }
            $this->updates('', 'cat_id='.$cat_id.' AND role_id='.$rid['rid']);
        }
    }

    public function getCategoryAuth(&$cat, $cat_id)
    {
        if (empty($cat)) return '';

        $rids  = roleModel::model()->findsByAttribute('rid');

        $ridstring = '';
        foreach ($rids as $rid) {
            $ridstring .= $rid['rid'].',';
        }
        $ridstring = substr($ridstring, 0, -1);

        $res = $this->finds('cat_id='.$cat_id.' AND role_id IN('.$ridstring.')');

        foreach ($res as $k => $r) {
            $cat['role_'.$r['role_id']] = array(
                $r['look'],
                $r['add'],
                $r['edit'],
                $r['delete'],
                $r['sort'],
                $r['move']
            );
        }
        unset($res); 
    }

}
?>